/*
* @Author: steven.wang
* @Date:   2019-02-27 10:53:23
* @Last Modified by:   steven.wang
* @Last Modified time: 2019-02-27 14:34:26
*/
const mysql = require('mysql');
const dbConfig = require('./config').mysql;

const pool = mysql.createPool({
	user: dbConfig.user,
	password: dbConfig.password,
	database: dbConfig.database,
	host: dbConfig.host,
	port: dbConfig.port,
	insecureAuth: true
});

const query = (sql, values) => new Promise((resolve, reject) => {
	pool.getConnection((err, connection) => {
		if (err) {
			reject(err);
		} else {
			connection.query(sql, values, (err, rows) => {
				if (err) {
					reject(err);
				}  else {
					resolve(rows);
				}
				connection.release();
			});
		}
	});
});

module.exports = query